package woda.ink.controller.palteform;

import com.alibaba.fastjson.JSON;
import groovy.util.logging.Commons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import woda.ink.entity.fileEntity;
import woda.ink.entity.loginEntity;
import woda.ink.service.plateFormServer;
import woda.ink.util.PageUtil;
import woda.ink.util.TablePage;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/Request")
public class BrokerControllRest {
    @Autowired
    plateFormServer plate;
    @Autowired
    PageUtil pageUtil;
    @Autowired
    plateFormServer plateFormServer;

    @RequestMapping(value = "/userLogin")
    public String userLogin(@RequestBody loginEntity loginInfo) {
        String count = plate.CountServer("select Count(*) as a from plateform_Employee where loginName = '"+loginInfo.getLoginName()+"'");
        if (Integer.parseInt(count) == 0){
            return "fails";
        }
        loginEntity result = plate.LoginServer(loginInfo.getLoginName());
        System.out.println(result.getLoginName());
        System.out.println(result.getPassword());
        if (result.getPassword().equals(loginInfo.getPassword())){
            return JSON.toJSONString(result.getPower());
        }else {
            return JSON.toJSONString("fail");
        }
    }

    @RequestMapping(value = "/updateOnefile", method = RequestMethod.POST)
    public String updateOnefile(@RequestBody fileEntity fileEntity) {
        int result = plate.UpdateOneFile(fileEntity);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/delSource")
    public String delSource(@RequestParam String sourseID) {
        int result = plate.deleteOne("update plateform_file set eStatus = 0 where sourseID = "+sourseID);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping("/staff_Employee")
    public String  staff_Employee(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize,@RequestParam(required = true)String loginName) {

        String sql = "select * from plateform_Employee where eStatus = 1 and loginName like '%" +loginName+"%'";
        System.out.println(sql);
        TablePage tp = pageUtil.getDataForPaging(plate.EmployeeServer(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }

    @RequestMapping(value = "/updateOneEmployee", method = RequestMethod.POST)
    public String updateCase(@RequestBody loginEntity loginEntity) {
        System.out.println(loginEntity.getPower());
        int result = plate.UpdateOneEmployee(loginEntity);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }
    @RequestMapping(value = "/delEmployee")
    public String delEmployee(@RequestParam String employeeID) {
        int result = plate.deleteOne("update plateform_Employee set eStatus = 0 where employeeID = "+employeeID);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/UpdateRead")
    public String UpdateRead(@RequestParam String sourseID,@RequestParam String username) {
       int result = plate.UpdateRead(sourseID,username);
        if(result>0) {
            return "true";
        }else{
            return "false";
        }
    }

    @RequestMapping(value = "/addEmployee")
    public String addEmployee(@RequestBody loginEntity loginEntity){
        String sql = "select Count(*) as a from plateform_Employee where loginName = '" + loginEntity.getLoginName()+"'";
        String count = plate.CountServer(sql);
        if (Integer.parseInt(count) > 0){
            return JSON.toJSONString ("already");
        }
        int result = plate.addEmployee(loginEntity);
        if (result > 0){
            return JSON.toJSONString ("true");
        }else {
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping("/EmployeeRecord")
    public String  EmployeeRecord(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize) {

        String sql = "select employeeID from plateform_Record where eStatus = 1   group  by employeeID";
        TablePage tp = pageUtil.getDataForPaging(plate.EmployeeRecord(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }

    @RequestMapping("/getArticle")
    public String  getArticle(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize,@RequestParam String ArticleType) {
        String sql = "select * from plateform_file where eStatus = 1 and type like '%Article%' and type like '%"+ArticleType+"%'  order by dtCreateTime desc";
        TablePage tp = pageUtil.getDataForPaging(plate.SourseServer(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }

    @RequestMapping("/getVideo")
    public String  getVideo(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam String VideoType) {


        String sql = "select * from plateform_file where eStatus = 1 and type like '%Video%' and type like '%"+VideoType+"%' order by dtCreateTime desc";
        TablePage tp = pageUtil.getDataForPaging(plate.SourseServer(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }

    @RequestMapping("/getCourseware")
    public String  getCourseware(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam String CoursewareType) {


        String sql = "select * from plateform_file where eStatus = 1 and type like '%Courseware%' and type like '%"+CoursewareType+"%' order by dtCreateTime desc";
        TablePage tp = pageUtil.getDataForPaging(plate.SourseServer(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }

    @RequestMapping("/getSystem")
    public String  getSystem(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize) {
/*        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        if (Startime == ""){
            c.setTime(new Date());
            c.add(Calendar.MONTH,-1);
            Date date = c.getTime();
            Startime = df.format(date);
        }
        if (Endtime ==""){

            Endtime = df.format(new Date());
            System.out.println(Endtime);
        }*/
        String sql = "select * from plateform_file where  eStatus = 1 and type like '%System%'  order by dtCreateTime desc";
        TablePage tp = pageUtil.getDataForPaging(plate.SourseServer(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }

    @RequestMapping("/Staff_ResourseList")
    public String  getStaff_SourseList(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam String type) {

        String sql = "select * from plateform_file where eStatus = 1 and type like '%"+type+"%' order by downCount desc";
        TablePage tp = pageUtil.getDataForPaging(plate.SourseServer(sql,pageNumber,pageSize));
        return JSON.toJSONString(tp);
    }
    @RequestMapping("/typeSelect")
    public String typeSelect(){
        TablePage tablePage = pageUtil.getDataForPaging(plateFormServer.selectType());
        return JSON.toJSONString(tablePage);
    }

}
